import {
	getWechatConfig
} from "@/api/public";
import {
	checkCard
} from "@/api/trading.js";
import wx from "weixin-js-sdk";
import util from "@/utils/util.js";
export function getWxConfig() {
	getWechatConfig().then((res) => {
		setWxConfig(res.result);
	});
}

function setWxConfig(data) {
	wx.config({
		debug: false,
		appId: data.appId,
		timestamp: data.timestamp,
		nonceStr: data.nonceStr,
		signature: data.signature,
		jsApiList: ["scanQRCode", "checkJsApi"],
	});
	wx.ready((r) => {
		wx.checkJsApi({
			jsApiList: ["scanQRCode"],
			success: function(res) {
				if (res.checkResult.getLocation == false) {
					alert("你的微信版本太低，不支持微信JS接口，请升级到最新的微信版本！");
					return;
				}
			},
		});
		wx.scanQRCode({
			needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果
			success: function(res) {
				uni.showLoading({
					title: "正在核销",
				});
				checkCard(res.resultStr)
					.then((e) => {
						util.Tips({
							title: "核销成功",
						});
						uni.hideLoading();
					})
					.catch((err) => {
						util.Tips({
							title: err,
						});
						uni.hideLoading();
					});
			},
			fail: (err) => {
				alert("出错" + JSON.stringify(err));
			},
		});
	});
	wx.error(function(res) {
		alert("error：" + res.errMsg);
	});
}